package com.zcf.service.impl;

import com.zcf.dto.CategoryDto;
import com.zcf.dto.UpdateCategoryDto;
import com.zcf.mapper.CategoryMapper;
import com.zcf.pojo.Category;
import com.zcf.pojo.Result;
import com.zcf.service.CategoryService;
import com.zcf.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author zcf
 */
@Service("categoryServiceImpl")
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public Result list() {

     List<Category> list= categoryMapper.list();
     return Result.success(list);
    }


    @Override
    public Result add(CategoryDto categoryDto) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDto, category);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        category.setCreateUser(id);
        categoryMapper.add(category);
        return Result.success();
    }

    @Override
    public Category findById(Integer id) {
       return categoryMapper.find(id);
    }



    @Override
    public Result update(UpdateCategoryDto updateCategoryDto) {
        Category category=new Category();
        BeanUtils.copyProperties(updateCategoryDto,category);
        category.setUpdateTime(LocalDateTime.now());

        categoryMapper.update(category);
        return Result.success();
    }



    @Override
    public Result delete(Integer id) {

       categoryMapper.delete(id);
       return Result.success();
    }


}
